package com.lyf.springmvc.cmd;

import com.lyf.springmvc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;


@Controller
@RequestMapping("/demo")
public class LuController {

    @RequestMapping(value="/handle",method ={RequestMethod.POST})
    public ModelAndView handle(String username) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", new Date().toString());
        modelAndView.addObject("username", username);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/handle01", method = {RequestMethod.POST})
    @ResponseBody
    public User handle01(@RequestBody  User user) {
        user.setUsername("是否加代理开");
        return user;
    }


    @RequestMapping("/tt")
    public ModelAndView test() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", new Date().toString());
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/t1")
    public String test1(ModelMap map) {
        map.addAttribute("date", new Date().toString());
        return "success";
    }

    @RequestMapping("/t2")
    public String test2(HashMap map) {
        map.put("date", new Date().toString());
        return "success";
    }

    /**
     * 参数为八大类型
     * @param id
     * @return
     */
    @RequestMapping("/t4")
    public ModelAndView test4(Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", new Date().toString());
        modelAndView.addObject("id", id);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /**
     * 自定义传入参数名，通过RequestParam进行转换
     * @param id
     * @return
     */
    @RequestMapping("/t3")
    public ModelAndView test3(@RequestParam("is") Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", new Date().toString());
        modelAndView.addObject("id", id);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /**
     * 传入参数为pojo类(要求传递的参数名必须和pojo类的成员变量名保持一致)
     * 链接：http://localhost:8080/demo/t5?id=1&username=sjdfdsf&role.rolename=rolesdf
     * @param user
     * @return
     */
    @RequestMapping("/t5")
    public ModelAndView test5(User user) throws UnsupportedEncodingException {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("id", user.getId());
        modelAndView.addObject("username",user.getUsername());

//        modelAndView.addObject("username", new String(user.getUsername().getBytes("iso8859-1"),"UTF-8"));
        modelAndView.addObject("role", user.getRole());
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /**
     * 绑定pojo包装类对象（即pojo的成员变量中有pojo类）
     * @param user
     * @return
     */
    @RequestMapping("/t6")
    public ModelAndView test6(User user) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("id", user.getId());
        modelAndView.addObject("username", user.getUsername());
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /**
     * 绑定日期
     * @param date
     * @return
     */
    @RequestMapping("/t7")
    public ModelAndView test7(Date date) {
        ModelAndView modelAndView = new ModelAndView();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
        modelAndView.addObject("date", sdf.format(date));
        modelAndView.setViewName("success");
        return modelAndView;
    }


}
